<template>
  <div id="userRegisterPage">
    <h2 style="margin-bottom: 16px">用户注册</h2>
    <a-form
      :model="form"
      :style="{ width: '480px', margin: '0 auto' }"
      label-align="left"
      auto-label-width
      @submit="handleSubmit"
    >
      <a-form-item field="userAccount" label="账号">
        <a-input v-model="form.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item field="userPassword" tooltip="密码不小于 4 位" label="密码">
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
        />
      </a-form-item>
      <a-form-item
        field="checkPassword"
        tooltip="确认密码不小于 4 位"
        label="确认密码"
      >
        <a-input-password
          v-model="form.checkPassword"
          placeholder="请输入确认密码"
        />
      </a-form-item>
      <a-form-item field="userName" tooltip="必填项" label="用户昵称">
        <a-input v-model="form.userName" placeholder="请输入用户昵称" />
      </a-form-item>
      <!--      <a-form-item field="department" tooltip="必选项" label="所属部门">-->
      <!--        <a-dropdown trigger="click" v-model="form.department">-->
      <!--          <a-button style="border: none; color: #000">-->
      <!--            {{ form.department || "请选择所属部门" }}-->
      <!--          </a-button>-->
      <!--          <template #content>-->
      <!--            <a-menu @select="handleSelect">-->
      <!--              <a-menu-item key="煤矿部门" value="采煤部门"-->
      <!--                >采煤部门</a-menu-item-->
      <!--              >-->
      <!--              <a-menu-item key="掘进部门" value="掘进部门"-->
      <!--                >掘进部门</a-menu-item-->
      <!--              >-->
      <!--              <a-menu-item key="运输部门" value="运输部门"-->
      <!--                >运输部门</a-menu-item-->
      <!--              >-->
      <!--            </a-menu>-->
      <!--          </template>-->
      <!--        </a-dropdown>-->
      <!--      </a-form-item>-->
      <a-form-item field="department" tooltip="必选项" label="所属部门">
        <a-cascader
          :options="departmentOptions"
          v-model="form.department"
          placeholder="请选择所属部门"
          :style="{ width: '100%' }"
        />
      </a-form-item>
      <a-form-item
        field="miniDepartment"
        tooltip="非必填项"
        label="所属次级部门"
      >
        <a-input
          v-model="form.miniDepartment"
          placeholder="请输入所属次级部门"
        />
      </a-form-item>

      <a-form-item>
        <div
          style="
            display: flex;
            width: 100%;
            align-items: center;
            justify-content: space-between;
          "
        >
          <a-button type="primary" html-type="submit" style="width: 120px">
            注册
          </a-button>
          <a-link href="/user/login">已注册用户登录</a-link>
        </div>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import API from "@/api";
import { userRegisterUsingPost } from "@/api/userController";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";

const departmentOptions = [
  {
    value: "采煤部门",
    label: "采煤部门",
  },
  {
    value: "掘进部门",
    label: "掘进部门",
  },
  {
    value: "运输部门",
    label: "运输部门",
  },
];

const router = useRouter();

const form = reactive({
  userAccount: "",
  userPassword: "",
  checkPassword: "",
  userName: "",
  department: "",
  miniDepartment: "",
} as API.UserRegisterRequest);

/**
 * 提交
 */
const handleSubmit = async () => {
  const res = await userRegisterUsingPost(form);
  if (res.data.code === 0) {
    message.success("注册成功");
    router.push({
      path: "/user/login",
      replace: true,
    });
  } else {
    message.error("注册失败，" + res.data.message);
  }
};
</script>
